using System;
using System.Text;
using System.Web;
using System.Configuration;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Security;
using System.Security.Principal;
using System.Web.Security;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Starznet.SharePoint.SPDAT.ApplicationPages.Settings;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Administration
{
    public class BaseMembershipPage : StarznetBaseAdministrationPage
    {
        private SPWebApplication _oWebApp;

        public override string PageToRedirectOnCancel
        {
            get
            {
                return "MembershipAppList.aspx";
            }
        }

        protected string WebAppUrl
        {
            get 
            {
                return Request.QueryString.Get("wapp"); 
            }
        }

        protected SPWebApplication WebApp
        {
            get 
            {
                if (_oWebApp == null) _oWebApp = SPWebApplication.Lookup(new Uri(Request.QueryString.Get("url")));

                return _oWebApp; 
            }
            set { _oWebApp = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
                this.SetCurrentMembershipSection();
        }

        private void SetCurrentMembershipSection()
        {
            IntPtr oUserToken = WindowsIdentity.GetCurrent().Token;

            Configuration oTargetConfig =
                WebConfigurationManager.OpenWebConfiguration("/", Request.QueryString.Get("wapp"), "", Environment.MachineName, oUserToken);
            MembershipSection oTargetMemSection = (MembershipSection)oTargetConfig.GetSection("system.web/membership");
            ProviderSettings oTargetSettings = oTargetMemSection.Providers[oTargetMemSection.DefaultProvider];
            RoleManagerSection oTargetRoleSection = (RoleManagerSection)oTargetConfig.GetSection("system.web/roleManager");
            ProviderSettings oTargetRoleSettings = oTargetRoleSection.Providers[oTargetRoleSection.DefaultProvider];
            
            Configuration oSourceConfig =
                WebConfigurationManager.OpenWebConfiguration("/");
            MembershipSection oSourceMemSection = (MembershipSection)oSourceConfig.GetSection("system.web/membership");
            ProviderSettings oSourceSettings = oSourceMemSection.Providers[oSourceMemSection.DefaultProvider];
            RoleManagerSection oSourceRoleSection = (RoleManagerSection)oSourceConfig.GetSection("system.web/roleManager");            
            ProviderSettings oSourceRoleSettings = oSourceRoleSection.Providers[oSourceRoleSection.DefaultProvider];

            foreach (string sParam in oTargetSettings.Parameters.Keys)
                oSourceSettings.Parameters[sParam] = oTargetSettings.Parameters[sParam];

            //oSourceRoleSection.Enabled = oTargetRoleSection.Enabled;

            foreach (string sParam in oTargetRoleSettings.Parameters.Keys)
                oSourceRoleSettings.Parameters[sParam] = oTargetRoleSettings.Parameters[sParam];

            foreach (ConnectionStringSettings oConn in oTargetConfig.ConnectionStrings.ConnectionStrings)
            {
                if (oSourceConfig.ConnectionStrings.ConnectionStrings[oConn.Name] == null)
                    oSourceConfig.ConnectionStrings.ConnectionStrings.Add(oConn);
            }

            oSourceConfig.Save();
        }
    }
}